package util.List2Json1;


import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.chauncy.DMS.bean.DormInfoBean;
import com.chauncy.DMS.dao.ManagerDaoImplement;




import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONBuilder;

public class List2Json1 {
	//未住满人员
	private static Long countItem = null ;
	//全部考勤记录
	private static Long countAllAtt = null;
	
	
	
	public static 	JSONArray List2Json(List<DormInfoBean> list){
		
		return JSONArray.fromObject(list);
	}
	
	// 未住满宿舍 list 转 json
	public static JSONObject listToJson(List<DormInfoBean> list,String buildings){
		ManagerDaoImplement daoImplement = new ManagerDaoImplement();
		String sql = "select COUNT(*) from (select Dorm_number," +
				"Checkin_number,Actual_number_of_people from dorm_info_table" +
				"  WHERE Checkin_number > Actual_number_of_people" +
				"  AND Buildings_number = ?) as sdp;";
		//item:[{}] 内容
		JSONArray array = new JSONArray();
		//主体JSON
		JSONObject jsonMain = new JSONObject();
		//Item:[{}]
		JSONObject jsonItem = new JSONObject();
		//楼栋号
		Object[] param = {buildings};
		 if(countItem == null){
			//获取总条目数
			countItem = daoImplement.count(sql, param);
		//	System.out.println("执行count");
		 }else{
			countItem = countItem;
		}
		//遍历list  取值
		for(DormInfoBean attBean : list){
			//获取item的值
			JSONObject object = new JSONObject();
			object.put("Dorm_number", attBean.getDorm_number());
			object.put("Checkin_number", attBean.getCheckin_number());
			object.put("Actual_people", attBean.getActual_number_of_people());
			object.put("dormPeople", (attBean.getCheckin_number()-attBean.getActual_number_of_people()));
			array.add(object);
		}
		//为item 添加值
		jsonItem.put("item", array);
		//JOSN 添加 status
		jsonMain.put("status", 200);
		//JOSN 添加 message
		jsonMain.put("message","");
		//JOSN 添加 datasize
		jsonMain.put("datasize",countItem );
		//JOSN 添加 rows rows为item
		jsonMain.put("rows", jsonItem);
		//System.out.println(jsonMain);
		return jsonMain;
	}
	
	/**
	 * map 转 JSON
	 * @param List<Map<String,Object>> 转入lsit, buildings(可以为null) 楼栋号 ,sql count语句
	 * @return JSONObject
	 * 格式：如上
	 */
	public static JSONObject mapToJSON(List<Map<String,Object>> listMap,Object [] param,String sql){
				ManagerDaoImplement daoImplement = new ManagerDaoImplement();
				
				countAllAtt = daoImplement.count(sql, param);
				//主体JSON
				JSONObject jsonMain = new JSONObject();
				//Item:[{}]
				JSONObject jsonItem = new JSONObject();
				//item:[{}] 内容
				JSONArray array = new JSONArray();
				
				for(Map<String, Object> map : listMap){
					JSONObject jsonObject =JSONObject.fromObject(map);
					array.add(jsonObject);
				}
				//为item 添加值
				jsonItem.put("item", array);
				//JOSN 添加 status
				jsonMain.put("status", 200);
				//JOSN 添加 message
				jsonMain.put("message","");
				//JOSN 添加 datasize
				jsonMain.put("datasize",countAllAtt);
				//System.out.println(countItem);
				//JOSN 添加 rows rows为item
				jsonMain.put("rows", jsonItem);
				//System.out.println(jsonMain);
		
		return jsonMain;
	}
	/**
	 * 
	 * @param listMap 需要转为JSONObject 的list
	 * @return JSONObject 
	 */
	public static JSONObject mapToJSON(List<Map<String,Object>> listMap){
		ManagerDaoImplement daoImplement = new ManagerDaoImplement();
	
		//System.out.println("countAllAtt:"+countAllAtt+"条记录");
		//主体JSON
		JSONObject jsonMain = new JSONObject();
		//Item:[{}]
		JSONObject jsonItem = new JSONObject();
		//item:[{}] 内容
		JSONArray array = new JSONArray();
		
		for(Map<String, Object> map : listMap){
			JSONObject jsonObject =JSONObject.fromObject(map);
			array.add(jsonObject);
		}
		//为item 添加值
		jsonItem.put("item", array);
		//JOSN 添加 status
		jsonMain.put("status", 200);
		//JOSN 添加 message
		jsonMain.put("message","");
		//JOSN 添加 datasize
		jsonMain.put("datasize",listMap.size());
		System.out.println("allAttInfo:"+listMap.size());
		//System.out.println(countItem);
		//JOSN 添加 rows rows为item
		jsonMain.put("rows", jsonItem);
		//System.out.println(jsonMain);

		return jsonMain;
}
	
	public JSONObject listMapToJson(List<Map<String,Object>> listMap) {
		
		return null;
		
	}
	
	public static String GetNowDate(){   
	    String temp_str="";   
	    Date dt = new Date();   
	    //最后的aa表示“上午”或“下午”    HH表示24小时制    如果换成hh表示12小时制   
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");   
	    temp_str=sdf.format(dt);   
	    return temp_str;   
	} 
}
